﻿using System;
using System.Collections.Generic;
using System.Text;
using Domain.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace Infrastructure.Data.Config
{
    public class GitLibraryConfiguration : IEntityTypeConfiguration<GitLibaray>
    {
        public void Configure(EntityTypeBuilder<GitLibaray> builder)
        {
            builder.HasKey(b => b.Id);

            builder.Ignore(p => p.Password);

            builder.Ignore(b => b.CreateDateTime);

            builder.Ignore(b => b.ModifyDateTime);

            builder.Ignore(b => b.RepositoryUrl);

            builder.Property(b => b.Name)
                .IsRequired()
                .HasMaxLength(50);

            builder.Property(b => b.Account)
                .IsRequired()
                .HasMaxLength(50);

            builder.HasIndex(b => b.Account)
                .IsUnique();

            builder.Property("_passwordHash")
                .HasColumnName("PasswordHash")
                .IsRequired()
                .HasMaxLength(32);

            builder.Property("_password")
                .HasColumnName("Password")
                .IsRequired()
                .HasMaxLength(256);

            builder.Property(b => b.SyncIntervalSecond)
                .HasColumnName("Interval")
                .IsRequired();

            builder.Property(b => b.Remember)
                .IsRequired();

            builder.Property("_repositoryUrl")
                .IsRequired()
                .HasColumnName("GitUrl")
                .HasMaxLength(500);

            builder.Property(b => b.LocalDirectory)
                .IsRequired()
                .HasMaxLength(500);

            builder.Metadata
                .FindNavigation(nameof(GitLibaray.Logs))
                .SetPropertyAccessMode(PropertyAccessMode.Field);

            builder.Property("_createDateTime")
                .HasColumnName("CreateDateTime")
                .HasDefaultValueSql("strftime('%s','now')");

            builder.Property("_modifyDateTime")
                .HasColumnName("ModifyDateTime")
                .HasDefaultValueSql("strftime('%s','now')");
        }
    }
}
